//下面两行是JS原生模块
var events = require("events");
var EventEmitter = events.EventEmitter;

function Dog(name, energy){
    EventEmitter.call(this);
    this.name = name;
    this.energy = energy;

/**
 * function函数内this指向取决于调用主体
 * 箭头函数内this指向函数定义的上下文
 */
    var intervalId = setInterval(() => {
        if(this.energy >= 0){
            this.emit("bark");
            this.energy--;
        }
        else{
            clearInterval(intervalId);
        }
    },1000)
}

for(var i in EventEmitter.prototype){
    Dog.prototype[i] = EventEmitter.prototype[i];
}

module.exports = Dog;